//
//  YCManageViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/20.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCManageViewController.h"
#define kBtnTag 111

@interface YCManageViewController ()
{
    UIScrollView *m_scrollView;
    UIImageView *m_logoView;
    UILabel *m_labTitle;
    
    UIButton *m_btnPrint;
    UIButton *m_btnSale;
    
    ShopTable *_shopTable;
    
    YCDataButton *but1;
    YCDataButton *but2;
    YCDataButton *but3;
    
    YCHomeButton *_button2;//评价按钮
    UILabel *m_labNotice;
    
    YCNoticeView *m_noticeView;
}
@end

@implementation YCManageViewController

-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:YES];
    
    [self loadShopData];
    [self loadPrint];
}
-(void)viewDidAppear:(BOOL)animated
{
    [self loadPrint];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[App shared]readShopData];
    _shopTable=[App shared].shop;
    [self addSubviews];
    [self loadShopData];
}
-(void)loadShopData
{
    ShopIndexRequest *request = [ShopIndexRequest new];
    [apiClient hideProgress];
    [apiClient doShopIndex:request success:^(ApiResponse *data, NSString *url) {
        ShopIndexResponse *response=[[ShopIndexResponse alloc]initWithCGResponse:data];
        _shopTable=response.data;
        [m_logoView load:_shopTable.logo];
        
        
        CGFloat titleWidth=kMainScreen_Width-160-4*kDistance-50-10;
        CGFloat nameH=getTextSize(_shopTable.name, titleWidth, [UIFont LightFontOfSize:15]).height;
        [m_labTitle setFrame:CGRectMake(CGRectGetMaxX(m_logoView.frame)+kDistance, 60, titleWidth, nameH)];
        
        [m_labTitle setText:_shopTable.name];
        [m_btnSale setSelected:_shopTable.open.intValue];
        
        [App shared].shop=_shopTable;
        [[App shared]saveShopData];
        
        [but1 setButtonWithTitle:@"今日营业额" Data:_shopTable.today_turnover Desc:[NSString stringWithFormat:@"昨日%@",_shopTable.yesterday_turnover]];
        [but2 setButtonWithTitle:@"今日订单" Data:_shopTable.today_order Desc:[NSString stringWithFormat:@"昨日%@",_shopTable.yesterday_order]];
        [but3 setButtonWithTitle:@"可用余额" Data:_shopTable.balance Desc:@""];
        
        [m_labNotice setText:_shopTable.notice_title];
        CGSize textSize=getTextSize(m_labNotice.text, W(m_labNotice), [UIFont LightFontOfSize:13]);
        if (textSize.height>H(m_labNotice)) {
            CGRect lframe = m_labNotice.frame;
            lframe.size.width = textSize.width;
            m_labNotice.frame = lframe;
            
            CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
            anim.duration = 10;
            anim.fromValue = @(0);
            anim.toValue = @(-200);
            anim.repeatCount = 100;
            [m_labNotice.layer addAnimation:anim forKey:nil];
        }

        [_button2 showNewComment:_shopTable.comment_new.intValue];
        
        [m_scrollView.mj_header endRefreshing];
    }failure:^(ApiResponse *data, NSString *url){
        [self.view showHUD:data.message];
        [m_scrollView.mj_header endRefreshing];
    }];
}
-(void)loadPrint
{
    NSUserDefaults  *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *uuidString = [userDefault stringForKey:@"uuidstring"];
    NSString *status=[userDefault stringForKey:@"printOn"];
    
    if ( (uuidString.length>0) && [status isEqualToString:@"yes"]) {
        [m_btnPrint setSelected:YES];
    }else{
        [m_btnPrint setSelected:NO];
    }
}
-(void)addSubviews
{
    m_scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, -20, kMainScreen_Width, kMainScreen_Height)];
    [m_scrollView setContentSize:CGSizeMake(kMainScreen_Width, kMainPage_Height+80)];
    m_scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:m_scrollView];
    
    
    CGFloat width =kMainScreen_Width-2*kDistance;
    
    UIView *backview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, 200)];
    [backview setBackgroundColor:MAIN_COLOR];
    [m_scrollView addSubview:backview];
    
    CGFloat orginY=60;
    m_logoView=[[UIImageView alloc]initWithFrame:CGRectMake(kDistance, orginY, 50, 50)];
    m_logoView.layer.cornerRadius=2.0f;
    m_logoView.layer.masksToBounds=YES;
    [m_logoView setBackgroundColor:WHITE_COLOR];
    [m_logoView setImage:[UIImage imageNamed:@"home_ysf"]];
    [backview addSubview:m_logoView];
    
    CGFloat titleWidth=kMainScreen_Width-160-4*kDistance-50-10;
    CGFloat height=getTextSize(_shopTable.name, titleWidth, [UIFont LightFontOfSize:15]).height;
    m_labTitle=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(m_logoView.frame)+kDistance, orginY, titleWidth, height)];
    [m_labTitle setFont:[UIFont LightFontOfSize:15]];
    [m_labTitle setNumberOfLines:0];
    [m_labTitle setTextColor:WHITE_COLOR];
    [backview addSubview:m_labTitle];
    
    
    m_btnSale=[[UIButton alloc]init];
    [m_btnSale setSelected:YES];
    [m_btnSale setTitle:@"已关店" forState:UIControlStateNormal];
    [m_btnSale setTitle:@"营业中" forState:UIControlStateSelected];
    [m_btnSale.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [m_btnSale setTitleColor:TEXT_MIDDLE forState:UIControlStateNormal];
    [m_btnSale setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
    [m_btnSale setImage:[UIImage imageNamed:@"home_open"] forState:UIControlStateSelected];
    [m_btnSale setImage:[UIImage imageNamed:@"home_close"] forState:UIControlStateNormal];
    [m_btnSale setBackgroundImage:[UIImage imageWithColor:GREEN_COLOR] forState:UIControlStateSelected];
    [m_btnSale setBackgroundImage:[UIImage imageWithColor:WHITE_COLOR] forState:UIControlStateNormal];
    [m_btnSale setImageEdgeInsets:UIEdgeInsetsMake(-3, -2, -3, 42)];
    [m_btnSale setTitleEdgeInsets:UIEdgeInsetsMake(0, -48, 0, 0)];
    [m_btnSale addTarget:self action:@selector(btnSaleClicked:) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:m_btnSale];
    [m_btnSale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backview).offset(-kDistance);
        make.top.equalTo(backview).offset(orginY);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(32);
    }];
    m_btnSale.layer.cornerRadius=2.0f;
    m_btnSale.layer.masksToBounds=YES;
    
    m_btnPrint=[[UIButton alloc]init];
    [m_btnPrint setTitle:@"未连接" forState:UIControlStateNormal];
    [m_btnPrint setTitle:@"已连接" forState:UIControlStateSelected];
    [m_btnPrint.titleLabel setFont:[UIFont LightFontOfSize:15]];
    [m_btnPrint setTitleColor:TEXT_MIDDLE forState:UIControlStateNormal];
    [m_btnPrint setTitleColor:WHITE_COLOR forState:UIControlStateSelected];
    [m_btnPrint setImage:[UIImage imageNamed:@"home_link"] forState:UIControlStateSelected];
    [m_btnPrint setImage:[UIImage imageNamed:@"home_unlink"] forState:UIControlStateNormal];
    [m_btnPrint setBackgroundImage:[UIImage imageWithColor:GREEN_COLOR] forState:UIControlStateSelected];
    [m_btnPrint setBackgroundImage:[UIImage imageWithColor:WHITE_COLOR] forState:UIControlStateNormal];
    [m_btnPrint setImageEdgeInsets:UIEdgeInsetsMake(-3, -2, -3, 42)];
    [m_btnPrint setTitleEdgeInsets:UIEdgeInsetsMake(0, -48, 0, 0)];
    [m_btnPrint addTarget:self action:@selector(btnPrintClicked:) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:m_btnPrint];
    [m_btnPrint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(m_btnSale.left).offset(-kSmallDistance);
        make.centerY.equalTo(m_btnSale.centerY);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(32);
    }];
    m_btnPrint.layer.cornerRadius=2.0f;
    m_btnPrint.layer.masksToBounds=YES;
    [self loadPrint];
    
    
    UIView *statusView=[[UIView alloc]initWithFrame:CGRectMake(kDistance, 150, kMainScreen_Width-2*kDistance, 107)];
    [statusView setBackgroundColor:WHITE_COLOR];
    [statusView addShadow];
    [m_scrollView addSubview:statusView];
    
    [statusView.layer addSublayer:getLine(width/3, width/3, 2*kSmallDistance, H(statusView)-2*kSmallDistance, LINE_COLOR)];
    [statusView.layer addSublayer:getLine(width*2/3, width*2/3, 2*kSmallDistance, H(statusView)-2*kSmallDistance, LINE_COLOR)];
    
    CGFloat butW=width/3;
    CGFloat butH=H(statusView);
    
    but1=[[YCDataButton alloc]initWithFrame:CGRectMake(0, 0, butW, butH)];
    but1.tag=kBtnTag+1;
    [but1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [but1 setButtonWithTitle:@"今日营业额" Data:@"00.00" Desc:@"昨日0.00"];
    [statusView addSubview:but1];
    
    but2=[[YCDataButton alloc]initWithFrame:CGRectMake(butW, 0, butW, butH)];
    but2.tag=kBtnTag+2;
    but2.isCenter=YES;
    [but2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [but2 setButtonWithTitle:@"今日订单" Data:@"20" Desc:@"昨日0"];
    [statusView addSubview:but2];
    
    
    but3=[[YCDataButton alloc]initWithFrame:CGRectMake(butW*2, 0, butW, butH)];
    but3.tag=kBtnTag+3;
    [but3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [but3 setButtonWithTitle:@"可用余额" Data:@"10" Desc:@""];
    [statusView addSubview:but3];
    
    UIView *remarkView=[[UIView alloc]initWithFrame:CGRectMake(kDistance, CGRectGetMaxY(statusView.frame)+kSmallDistance, kMainScreen_Width-2*kDistance, 40)];
    [remarkView setBackgroundColor:WHITE_COLOR];
    [remarkView addShadow];
    [m_scrollView addSubview:remarkView];
    [remarkView click:self action:@selector(clickNotice:)];
    
    UIImageView *noticeView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 25/2, 15, 15)];
    [noticeView setImage:[UIImage imageNamed:@"home_remark"]];
    [remarkView addSubview:noticeView];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(35, 0, width-50, 40)];
    bgView.clipsToBounds = YES;
    [remarkView addSubview:bgView];
    
    CGFloat labWidth=width-50;
    m_labNotice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, labWidth, 40)];
    [m_labNotice setTextColor:MAIN_COLOR];
    [m_labNotice setFont:[UIFont LightFontOfSize:13]];
    [bgView addSubview:m_labNotice];
    
    UIView *typeView=[[UIView alloc]initWithFrame:CGRectMake(kDistance, CGRectGetMaxY(remarkView.frame)+kSmallDistance, kMainScreen_Width-2*kDistance, 215)];
    [typeView setBackgroundColor:WHITE_COLOR];
    [typeView addShadow];
    [m_scrollView addSubview:typeView];
    
    
    CGFloat w=W(typeView);
    CGFloat h=H(typeView);
    [typeView.layer addSublayer:getLine(kDistance, w-kDistance, h/2, h/2, LINE_COLOR)];
    [typeView.layer addSublayer:getLine(w/3, w/3, kSmallDistance, h-kSmallDistance, LINE_COLOR)];
    [typeView.layer addSublayer:getLine(w*2/3, w*2/3, kSmallDistance, h-kSmallDistance, LINE_COLOR)];
    
    CGFloat btnW=width/3;
    CGFloat btnH=h/2;
    
    YCHomeButton *button1=[[YCHomeButton alloc]initWithFrame:CGRectMake(0, 0, btnW, btnH)];
    button1.tag=1;
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button1 setButtonWith:@"商品" Image:@"home_goods"];
    [typeView addSubview:button1];
    
    _button2=[[YCHomeButton alloc]initWithFrame:CGRectMake(btnW, 0, btnW, btnH)];
    _button2.tag=2;
    [_button2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_button2 setButtonWith:@"评价" Image:@"home_comment"];
    [typeView addSubview:_button2];
    
    
    YCHomeButton *button3=[[YCHomeButton alloc]initWithFrame:CGRectMake(btnW*2, 0, btnW, btnH)];
    button3.tag=3;
    [button3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button3 setButtonWith:@"营业数据" Image:@"home_data"];
    [typeView addSubview:button3];
    
    
    YCHomeButton *button4=[[YCHomeButton alloc]initWithFrame:CGRectMake(0, btnH, btnW, btnH)];
    button4.tag=4;
    [button4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button4 setButtonWith:@"门店信息" Image:@"home_shop"];
    [typeView addSubview:button4];
    
    
    m_scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadShopData];
    }];

}
-(void)buttonClicked:(YCHomeButton *)button
{
    if (button.tag==1) {
        YCGoodsManageViewController *goodVC=[[YCGoodsManageViewController alloc]init];
        [self showNavigationView:goodVC];
    }
    if (button.tag==2) {
        YCCommentsViewController *commentVC=[[YCCommentsViewController alloc]init];
        [self showNavigationView:commentVC];
    }
    if (button.tag==3) {
        YCEarningViewController *earnVC=[[YCEarningViewController alloc]init];
        [self showNavigationView:earnVC];
    }
    if (button.tag==4) {
        YCShopMessageViewController *messageVC=[[YCShopMessageViewController alloc]init];
        [self showNavigationView:messageVC];
    }

}
-(void)btnPrintClicked:(UIButton *)button
{
    if (button.selected) {
        CBWAlertView *view = [[CBWAlertView alloc]initWithTitle:@"您确定要断开打印机连接吗？" andMessage:@"断开后将无法打印订单"];
        view.animationType = AnimationTypeBigToSmall;
        view.titleTextColor = TEXT_DEEP;
        [view addButtonWithTitle:@"取消" color:BLUE_COLOR handler:^(CBWAlertView *alertView){
        }];
        [view addButtonWithTitle:@"确定" color:BLUE_COLOR handler:^(CBWAlertView *alertView) {
            
            
            NSUserDefaults  *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setValue:@"no" forKey:@"printOn"];
            [userDefault setValue:@"" forKey:@"uuidstring"];
            [button setSelected:NO];
        }];
        [view show];
    }else{
        //[button setSelected:YES];
        YCConnectViewController *connectVC=[[YCConnectViewController alloc]init];
        [self showNavigationView:connectVC];
    }
}
-(void)btnSaleClicked:(UIButton *)button
{
    if (button.selected) {
        CBWAlertView *view = [[CBWAlertView alloc]initWithTitle:@"您确定要暂停营业吗？" andMessage:nil];
        view.animationType = AnimationTypeBigToSmall;
        view.titleTextColor = TEXT_DEEP;
        [view addButtonWithTitle:@"取消" color:BLUE_COLOR handler:^(CBWAlertView *alertView){
        }];
        [view addButtonWithTitle:@"确定" color:BLUE_COLOR handler:^(CBWAlertView *alertView) {
            [self loadUpdateData];
        }];
        [view show];
    }else{
        [self loadUpdateData];
    }
}

-(void)loadUpdateData
{
    ShopUpdateRequest *request = [ShopUpdateRequest new];
    [apiClient hideProgress];
    request.open=[NSString stringWithFormat:@"%d",!m_btnSale.selected];
    [apiClient doShopUpdate:request success:^(ApiResponse *data, NSString *url) {
        m_btnSale.selected=!m_btnSale.selected;
    }failure:^(ApiResponse *data, NSString *url){
    }];
    
}
-(void)clickNotice:(id)sender
{
    if (_shopTable.notice_type.intValue==1) {
        [self showNoticeView];
    }
    if (_shopTable.notice_type.intValue==2) {
        WebHtmlViewController *webVC=[[WebHtmlViewController alloc]initWithTitle:_shopTable.notice_title withUrl:_shopTable.notice_url];
        [self showNavigationView:webVC];
    }
}
-(void)showNoticeView
{
    if (_shopTable.notice_content.length==0) {
        return;
    }
    if (m_noticeView==nil) {
        m_noticeView=[[YCNoticeView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainPage_Height)];
    }
    [m_noticeView reloadWith:_shopTable];
    [m_noticeView show];
}


@end











